<!DOCTYPE html>
<html lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<meta charset="utf-8">
	<title></title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
	<meta name="description" content="">
	<meta name="author" content="">
	<!-- STYLESHEETS --><!--[if lt IE 9]><script src="../../js/flot/excanvas.min.js"></script><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->
	<link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css" >
	<link rel="stylesheet" type="text/css"  href="../../css/themes/default.css" id="skin-switcher" >
	<link rel="stylesheet" type="text/css"  href="../../css/responsive.css" >	
	<link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
	<!-- SELECT2 -->
	<link rel="stylesheet" type="text/css" href="../../js/select2/select2.min.css" />
	<!-- UNIFORM -->
	<link rel="stylesheet" type="text/css" href="../../js/uniform/css/uniform.default.min.css" />
	<!-- datatable -->
    <link rel="stylesheet" href="../../js/datatables/datatable.css">
    <link rel="stylesheet" href="../../js/datepicker/datepicker.css">
	<!-- FONTS 
	<link href='http://fonts.useso.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
-->
</head>
<body>
	<header class="navbar clearfix" id="header">
	</header>
	
	<!-- PAGE -->
	<section id="page">
				<!-- SIDEBAR -->
				<div id="sidebar" class="sidebar">
					<div class="sidebar-menu nav-collapse">
						
						<!-- SIDEBAR MENU -->
						<ul>							
							
						</ul>
						<!-- /SIDEBAR MENU -->
					</div>

				</div>
				<!-- /SIDEBAR -->
		<div id="main-content">
			
			<div class="container">
				<div class="row">
					<div id="content" class="col-lg-12">
						<!-- PAGE HEADER-->
						<div class="row">
							<div class="col-sm-12">
								<div class="page-header">
									<!-- STYLER -->
									
									<!-- /STYLER -->
									<!-- BREADCRUMBS -->
									<ul class="breadcrumb">
										<li>
											<i class="fa fa-home"></i>
											<a href="index.html">首页</a>
										</li>
										<li>
											<a href="#">权限管理</a>
										</li>
										<li>添加角色</li>
									</ul>
									<!-- /BREADCRUMBS -->
									
								</div>
							</div>
						</div>
						<!-- /PAGE HEADER -->
						<!-- FORMS QUERY-->
						<div class="row">
							<div class="col-md-12">
								<div class="row">
									<div class="col-md-12">
										<div class="box border primary">
											<div class="box-title">
												<h4><i class="fa fa-bars"></i>角色维护</h4>
											</div>
											<div class="box-body big" style="padding:10px;">												
												<form class="form-horizontal" role="form" id="form_User">
												  <div class="form-group mrg-bt10">
													<label class="col-sm-1 control-label" style="width: auto"><span class="required">*</span>角色名称：</label>
													<div class="col-sm-2">
													  <input type="text" class="form-control" name="rolename" placeholder="输入角色名称">
													  <input type="hidden" name="id">
													</div>
													
												  </div>
												  <hr>
												 <div id="menu_list">
												  </div>
												  <hr>
												  <div class="row">
												<div class="col-md-12" style="text-align: center;">
													<div class="box ">
														<a href="#"  class="btn btn-success config bt-submit" style="width: 100px">
															保存
														</a>
													</div>
												</div>
												 </form>
											</div>
										</div>
									</div>
								</div>		
						    </div>
						</div>
						<!-- /FORMS QUERY -->
						
						
						
						<div class="footer-tools">
							<span class="go-top">
								<i class="fa fa-chevron-up"></i> Top
							</span>
						</div>
					</div><!-- /CONTENT-->
				</div>
			</div>
		</div>
	</section>
	<!--/PAGE -->
	<!-- JAVASCRIPTS -->
	<!-- Placed at the end of the document so the pages load faster -->
	<!-- JQUERY -->
	<script src="../../js/jquery/jquery-2.0.3.min.js"></script>
	
	<!-- BOOTSTRAP -->
	<script src="../../bootstrap-dist/js/bootstrap.min.js"></script>
	<!-- BLOCK UI -->
	<script type="text/javascript" src="../../js/jQuery-BlockUI/jquery.blockUI.min.js"></script>
	
	<!-- AUTOSIZE -->
	<script type="text/javascript" src="../../js/autosize/jquery.autosize.min.js"></script>
	<!-- spinner -->
		<script src="../../js/spinner/spin.js"></script>
	

	<!-- INPUT MASK -->
	<script type="text/javascript" src="../../js/bootstrap-inputmask/bootstrap-inputmask.min.js"></script>
   	
	<!-- SELECT2 -->
	<script type="text/javascript" src="../../js/select2/select2.min.js"></script>
	<!-- UNIFORM -->
	<script type="text/javascript" src="../../js/uniform/jquery.uniform.min.js"></script>
		<script type="text/javascript" src="../../js/datepicker/datepicker.js"></script>
		  <!--bootbox-->
	<script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
	<!-- COOKIE -->
	<script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
	<!-- CUSTOM SCRIPT -->
	<script src="../../js/script.js"></script>
	<script src="../../js/common.js"></script>
		<script>		
		var Role=function(){
	         var  $mainDataTable=null; 
	         var flag,id;
	    
	         
	         var savePermission=function(){
	            $("[name=rolename]").parents(".form-group").removeClass("has-error");
	         	if($("[name=rolename]").val()==""){
	         		$("[name=rolename]").parents(".form-group").addClass("has-error");
	         		return false;
	         	};
	         	
	         	//判断是否有选择
	         	var selectId=[];
	         	$("input[type=checkbox]:checked").each(function(){
					selectId.push($(this).val());
				})
				if(selectId.length==0)
				{
					$.alert("请至少选择一个权限");
					return false;
				}
				var role={};
				role.id=id;
				role.name=$("[name=rolename]").val();
				var url=App.getContextPath()+"admin/sysrole/saveRole.do";
				if(id!=null)
				  	url=App.getContextPath()+"admin/sysrole/updateRole.do";
	         		$.ajax({
						type: "post",
						url: url,
						async: true,
						data: {role:JSON.stringify(role),permissionIds:selectId.toString()},
						success: function(result) {
							location.href="role_list.html";
							}
					     
					   });
	         	
	         	
	         };
	         //checkbox 选择
	         var checkChange=function(){
	            
	            if($(this).attr("parentId")=="1"){
	             $(this).closest(".form-group").find("input[type=checkbox]").prop("checked" ,this.checked ? true :false);	        			
	              $.uniform.update($(this).closest(".form-group").find("input[type=checkbox]"));
	            }
	         };
	         
	         var searchRole=function(){	         
	             var url=App.getContextPath()+"admin/sysrole/getAllPermission.do";
			       $.ajax({
						type: "get",
						url: url,
						async: false,					
						success: function(result) {
					        var str="";
					        var buttonMenu=[];
					       $.each(result,function(index,item){
					           if(item.parentId==1){
					             var  parentId=item.id;
					             str="<div class='form-group mrg-bt10' style='height:auto;'><div class='col-md-4'><label class='checkbox' style='margin-left:30%'> "+
										"<input type='checkbox' class='uniform'  id='permission"+item.id+"' value="+item.id+" parentId="+item.parentId+"><span style='margin-left:15px;height:34px;margin-top:10px'>"+item.name+"</span></label></div>";
								 var child="";	
					             $.each(result,function(index,item){
					             	if(item.parentId==parentId)
					             	{
					             	   child+="<label class='checkbox'> <input id='permission"+item.id+"' type='checkbox' class='uniform'  name='permission' value="+item.id+" parentId="+item.parentId+"><span style='margin-left:15px;height:34px;margin-top:10px'>"+item.name+"</span></label> ";
					             	
					             	}
					             	
					                
					              });
					            
					           str+=" <div class='col-sm-8'>"+child+"</div></div>";
					           
					            $("#menu_list").append(str);
					           }
					       });
					      	
					       //循环button
					      
					       $.each(result,function(index,item){
					    	   if(item.type=="Button"){
					        	var but="<label class='checkbox' style='margin-left:30px'> <input id='permission"+item.id+"' type='checkbox' class='uniform'  name='permission' value="+item.id+" parentId="+item.parentId+"><span style='margin-left:15px;height:34px;margin-top:10px'>"+item.name+"</span></label>";
					        	$("#permission"+item.parentId).parent().after().append(but);
					        			
					    	   }
					        	
					        });
					       
					       $(".uniform").uniform();
					       $(".uniform").change(checkChange);
						}
					});
	         };
	         
	         var getRole=function(){
		         $.ajax({
							type: "get",
							url: App.getContextPath()+"admin/sysrole/getRole.do",
							async: true,
							data: {id:id},
							success: function(result) {
								if(result!=null)
								{
								  $("[name=rolename]").val(result.name);
								  //循环权限
								  $.each(result.permissions,function(index,item){
								 
								     $("#permission"+item.id).prop("checked",true);								      
								     
								  });
								   $.uniform.update($("input[type=checkbox]"));
								 }
								}
						     
						   });
	         
	         }
	       
	         
	         return {	         
		         init:function(){		            
		           searchRole();
		           $(".bt-submit").click(savePermission); 
		           flag=$.getUrlParam("flag");
		           id=$.getUrlParam("id");
		           if(flag=="edit")getRole();
		          }
		    }
	      }();
	      
	      $(function(){
	      	$("#header").load("../head.html");
	        App.init(); //Initialise plugins and elements	        
	      	Role.init();
	      	
	      });
		</script>
		<!-- /JAVASCRIPTS -->
</body>
</html>